package com.chinawayltd.ews.sample.service.algorithm.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chinawayltd.ews.sample.mapper.TTemplateEventMapper;
import com.chinawayltd.ews.sample.mapper.TTemplateMapper;
import com.chinawayltd.ews.sample.mapper.TTemplateNodeMapper;
import com.chinawayltd.ews.sample.mapper.algorithm.TemplateExtendMapper;
import com.chinawayltd.ews.sample.model.TTemplate;
import com.chinawayltd.ews.sample.model.TTemplateEvent;
import com.chinawayltd.ews.sample.model.TTemplateNode;
import com.chinawayltd.ews.sample.model.algorithm.TemplateVO;
import com.chinawayltd.ews.sample.service.algorithm.TemplateService;
import com.chinawayltd.ews.sample.web.query.TemplateGirdQuery;

/**
 * @program: greeProject
 * @description 流程模板、流程节点和异常事件逻辑实现类
 * @author: chenxin
 * @create: 2018年4月12日 下午6:20:20
 */
@Service
public class TemplateServiceImpl implements TemplateService{

	@Autowired
	TTemplateMapper tTemplateMapper;
    @Autowired
    TTemplateNodeMapper templateNodeMapper;
    @Autowired
    TTemplateEventMapper tTemplateEventMapper;
    @Autowired
    TemplateExtendMapper templateExtendMapper;
    
	@Override
	@Transactional
	public int add(TTemplate tEmplate) {
		tEmplate.setCreateTime(new Date());
		return tTemplateMapper.insertSelective(tEmplate);
	}

	@Override
	@Transactional
	public int update(TTemplate tEmplate) {
		tEmplate.setUpdateTime(new Date());
		return tTemplateMapper.updateByPrimaryKeySelective(tEmplate);
	}
 
	@Override
	public int delete(long id) {
		return templateNodeMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<TemplateVO> list(TemplateGirdQuery queryParam) {
		return templateExtendMapper.list(queryParam);
	}

	@Override
	public TTemplate getTemplateById(Long id) throws Exception{
		if(id == null || id <= 0){
            throw new RuntimeException("id不能为空或者少于等于0！");
        }
		return tTemplateMapper.selectByPrimaryKey(id);
	}

	@Override
	public TTemplateNode getTemplateNodeById(Long id) throws Exception{
		if(id == null || id <= 0){
            throw new RuntimeException("id不能为空或者少于等于0！");
        }
		return templateNodeMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<TTemplateNode> getTemplateNodeByTemplateId(Long id) {
		return templateExtendMapper.getTemplateNodeByTemplateId(id);
	}

	@Override
	public List<TTemplateEvent> getTemplateEventByTemplateId(Long id) {
		return templateExtendMapper.getTemplateEventByTemplateId(id);
	}

	@Override
	public int addEvent(TTemplateEvent tTemplateEvent) {
		tTemplateEvent.setCreateTime(new Date());
		return tTemplateEventMapper.insert(tTemplateEvent);
	}

	@Override
	@Transactional
	public int updateEvent(TTemplateEvent tTemplateEvent) {
		tTemplateEvent.setUpdateTime(new Date());
		return tTemplateEventMapper.updateByPrimaryKeySelective(tTemplateEvent);
	}

	@Override
	public int deleteEvent(Long id) throws Exception{
		if(id == null || id <= 0){
            throw new RuntimeException("id不能为空或者少于等于0！");
        }
		return tTemplateEventMapper.deleteByPrimaryKey(id);
	}

	@Override
	@Transactional
	public int updateEventState(Long id, Long enableState) {
		TTemplateEvent event = new TTemplateEvent();
		if(enableState ==1){
			event.setEnableState((byte)1);
		}else{
			event.setEnableState((byte)0);
		}
		event.setId(id);
		event.setUpdateTime(new Date());
		return tTemplateEventMapper.updateByPrimaryKeySelective(event);
	}
}
